<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" class="flex1" placeholder="请选择类型" style="width: 100%;">
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:divine:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:divine:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:divine:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="divineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <div v-if="scope.row.type == 1">卡牌问答</div>
          <div v-if="scope.row.type == 2">骰子问答</div>
          <div v-if="scope.row.type == 3">星盘问答</div>
          <div v-if="scope.row.type == 4">六爻</div>
        </template>
      </el-table-column>
      <el-table-column label="编码" align="center" prop="extend2" />
      <!-- 图片 scope.row.extend.img-->
      <el-table-column label="图片" align="center">
        <template slot-scope="scope">
          <el-image style="width: 40px; height: 40px;margin-right: 5px;" :src="JSON.parse(scope.row.extend).img"
            @click="Screen(JSON.parse(scope.row.extend).img)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="数据" align="center">
        <template slot-scope="scope">
          <!-- <el-image style="width: 20px; height: 20px;margin-right: 5px;" :src=""
             @click="Screen(item)"></el-image> -->
          <div v-for="(item, index) in JSON.parse(scope.row.extend).data" :key="index">
            {{ item.name }}:{{ item.value }}
          </div>
          <div v-for="(item, index) in JSON.parse(scope.row.extend).data1" :key="index">
            {{ item.name }}:{{ item.value }}
          </div>
          <div v-for="(item, index) in JSON.parse(scope.row.extend).data2" :key="index">
            {{ item.name }}:{{ item.value }}
          </div>
          <div>
            {{ JSON.parse(scope.row.extend).title }}
          </div>
          <div>
            {{ JSON.parse(scope.row.extend).content }}
          </div>
          <div>
            {{ JSON.parse(scope.row.extend).lable }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:divine:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:divine:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改系统占卜配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" class="flex1" placeholder="请选择类型" style="width: 100%;">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 编码 -->
        <el-form-item label="编码" prop="extend2">
          <el-input v-model="form.extend2" placeholder="请输入编码"></el-input>
        </el-form-item>
        <!-- 卡牌问答 -->
        <el-form-item label="卡牌问答" v-if="form.type == 1">
          <el-upload class="avatar-uploader" action="" :http-request="ossUploadFun" :show-file-list="false">
            <img v-if="kp.img" :src="kp.img" style="width: 100%;height: 100px;" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <div v-for="(item, index) in kpList" :key="index">
            <div class="title">内容{{ index + 1 }}</div>
            <div style="display: flex;">
              <el-input v-model="item.name" placeholder="请输入卡牌问题"></el-input>
              <el-input v-model="item.value" placeholder="请输入卡牌答案" style="margin-left: 5px;"></el-input>
              <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="kpList.splice(index, 1)"
                v-if="kpList.length > 1" style="margin-left: 5px;">删除</el-button>
            </div>
          </div>
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addKp">添加</el-button>
        </el-form-item>
        <!-- 骰子问答 -->
        <el-form-item label="骰子问答" v-if="form.type == 2">
          <el-input class="mb-5" v-model="dice.title" placeholder="请输入骰子标题"></el-input>
          <el-input class="mb-5" v-model="dice.content" placeholder="请输入骰子内容"></el-input>
          <el-input v-model="dice.lable" placeholder="请输入骰子标签以，分隔"></el-input>
        </el-form-item>
        <!-- 星盘问答 -->
        <el-form-item label="星盘问答" v-if="form.type == 3">
          <el-input v-model="astrolabe.content" placeholder="请输入星盘问答"></el-input>
        </el-form-item>
        <!-- 六爻 -->
        <el-form-item label="标题" v-if="form.type == 4">
          <el-input v-model="hexagram.title" placeholder="请输入六爻标题"></el-input>
          <div v-for="(item, index1) in hexagram1List" :key="index1">
            <div class="title">占卜{{ index1 + 1 }}</div>
            <div style="display: flex;">
              <el-input v-model="item.name" placeholder="请输入六爻问题"></el-input>
              <el-input v-model="item.value" placeholder="请输入六爻答案" style="margin-left: 5px;"></el-input>
			  <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="hexagram1List.splice(index, 1)"
			    v-if="hexagram1List.length > 1" style="margin-left: 5px;">删除</el-button>
            </div>
          </div>
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addHexagram(1)">添加</el-button>

          <div v-for="(item, index2) in hexagram2List" :key="index2">
            <div class="title">正反{{ index2 + 1 }}</div>
            <div>
				<div style="display: flex;">
					<el-input v-model="item.name" placeholder="请输入六爻问题"></el-input>
					<el-input v-model="item.value" placeholder="请输入六爻答案" style="margin-left: 5px;"></el-input>
					<el-button type="danger" plain icon="el-icon-delete" size="mini" @click="hexagram2List.splice(index, 1)"
					  v-if="hexagram2List.length > 1" style="margin-left: 5px;">删除</el-button>
				</div>
				<div style="display: flex;margin-top: 5px;">
					<el-select v-model="item.progress" class="flex1" placeholder="请选择正反" style="width: 100%;">
					  <el-option v-for="item in progressList" :key="item.value" :label="item.label" :value="item.value">
					  </el-option>
					</el-select>
					  <el-input v-model="item.remark" placeholder="请输入六爻备注" style="margin-left: 5px;"></el-input>
				</div>
            </div>
          </div>
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addHexagram(2)">添加</el-button>

          <div v-for="(item, index3) in hexagram3List" :key="index3">
            <div class="title">六冲卦{{ index3 + 1 }}</div>
            <div style="display: flex;">
              <el-input v-model="item.name" placeholder="请输入六爻问题"></el-input>
              <el-input v-model="item.value" placeholder="请输入六爻答案" style="margin-left: 5px;"></el-input>
			  <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="hexagram3List.splice(index, 1)"
			    v-if="hexagram3List.length > 1" style="margin-left: 5px;">删除</el-button>
            </div>
          </div>
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addHexagram(3)">添加</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDivine, getDivine, delDivine, addDivine, updateDivine } from "@/api/system/divine";

export default {
  name: "Divine",
  data() {
    return {
	progressList:[
		{
			value:1,
			label:"正"
		},
		{
			value:0,
			label:"反"
		}
	],
      // 六爻
      hexagram: {
        title: "",
        name: "",
        value: "",
		progress:1
      },
      // 六爻1列表
      hexagram1List: [{}],
      // 六爻2列表
      hexagram2List: [{}],
      // 六爻3列表
      hexagram3List: [{}],
      // 星盘问答
      astrolabe: {
        content: ""
      },
      // 骰子
      dice: {
        title: "",
        content: "",
        lable: ""
      },
      // 卡牌列表
      kpList: [{}],
      // 卡牌
      kp: {
        name: "",
        value: "",
        img: ''
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统占卜配置表格数据
      divineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: null,
        type: null,
        extend: null,
        extend2: null,
        extend3: null,
      },
      // 表单参数
      form: {
        id: null,
        businessId: null,
        type: null,
        extend: null,
        extend2: null,
        extend3: null,
        createTime: null
      },
      typeOptions: [{
        value: 1,
        label: '卡牌问答'
      }, {
        value: 2,
        label: '骰子问答'
      }, {
        value: 3,
        label: '星盘问答'
      }, {
        value: 4,
        label: '六爻'
      }],
      // 表单校验
      rules: {
        type: [{
          required: true,
          message: "请选择类型",
          trigger: "blur"
        }],
        extend2: [{
          required: true,
          message: "请输入编码",
          trigger: "blur"
        }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 图片上传
    ossUploadFun(file) {
      this.ossUpload(file.file).then(res => {
        this.kp.img = res;
      })
    },
    addHexagram(type) {
	const title = this.hexagram.title;
      this.hexagram = {
        name: "",
        value: "",
		title:JSON.parse(JSON.stringify(title))
      };

      if (type == 1) {
        this.hexagram1List.push({
          name: "",
          value: "",
        });
      }
      if (type == 2) {
        this.hexagram2List.push({
          name: "",
          value: "",
		  progress:1
        });
      }
      if (type == 3) {
        this.hexagram3List.push({
          name: "",
          value: "",
        });
      }
    },
    addKp(img) {
       let kp1 = {
        name: "",
        value: ""
      };
      this.kpList.push(kp1);
    },
    // 图片预览
    Screen(item) {
      this.$alert('<img src="' + item + '" style="width: 100%;height: 100%;"/>', '图片预览', {
        dangerouslyUseHTMLString: true,
        showConfirmButton: false,
        center: true,
        customClass: 'screen'
      })
    },
    /** 查询系统占卜配置列表 */
    getList() {
      this.loading = true;
      listDivine(this.queryParams).then(response => {
        this.divineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessId: null,
        type: null,
        extend: null,
        extend2: null,
        extend3: null,
        createTime: null
      };
      this.kpList = [{}];
      this.kp = {
        name: "",
        value: "",
        img: ''
      };
      this.dice = {
        title: "",
        content: "",
        lable: ""
      };
      this.astrolabe = {
        content: ""
      };
      this.hexagram = {
        title: "",
        name: "",
        value: "",
		progress:1
      };
	  // 六爻1列表
	  this.hexagram1List = [{}];
	  // 六爻2列表
	  this.hexagram2List =[{}];
	  // 六爻3列表
	  this.hexagram3List = [{}];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统占卜配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.form = {
        type: row.type,
        id: row.id,
        extend: row.extend,
        extend2: row.extend2,
      }

      // 如果type为1，卡牌问答
      if (row.type == 1) {
        this.kpList = JSON.parse(row.extend).data;
        this.kp.img = JSON.parse(row.extend).img;
      }

      // 如果type为2，骰子问答
      if (row.type == 2) {
        const extend = JSON.parse(row.extend);
        this.dice.title = extend.title;
        this.dice.content = extend.content;
        this.dice.lable = extend.lable;
      }

      // 如果type为3，星盘问答
      if (row.type == 3) {
        this.astrolabe.content = JSON.parse(row.extend).content;
      }

      // 如果type为4，六爻
      if (row.type == 4) {
        const extend = JSON.parse(row.extend);
        this.hexagram.title = extend.title;
        this.hexagram1List = extend.data;
        this.hexagram2List = extend.data1;
        this.hexagram3List = extend.data2;
      }

      const id = row.id || this.ids
      getDivine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改系统占卜配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          let extend = {};
          // 如果type为1，卡牌问答
          if (this.form.type == 1) {
            extend = {
              data: this.kpList,
              img: this.kp.img
            }
          }

          // 如果type为2，骰子问答
          if (this.form.type == 2) {
            // 需要校验title、content、lable是否为空用element的校验
            extend = {
              title: this.dice.title,
              content: this.dice.content,
              lable: this.dice.lable
            }
          }

          // 如果type为3，星盘问答
          if (this.form.type == 3) {
            extend = {
              content: this.astrolabe.content
            }
          }

          // 如果type为4，六爻
          if (this.form.type == 4) {
            extend = {
              title: this.hexagram.title,
              data: this.hexagram1List,
              data1: this.hexagram2List,
              data2: this.hexagram3List
            }
          }

          if (this.form.id != null) {
            updateDivine({
              id: this.form.id,
              type: this.form.type,
              extend2: this.form.extend2,
              extend: JSON.stringify(extend)
            }).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
			  this.reset();
              this.getList();
            });

          } else {
            addDivine({
              type: this.form.type,
              extend2: this.form.extend2,
              extend: JSON.stringify(extend)
            }).then(response => {
			this.reset();
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除系统占卜配置编号为"' + ids + '"的数据项？').then(function () {
        return delDivine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/divine/export', {
        ...this.queryParams
      }, `divine_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.mb-5 {
  margin-bottom: 5px;
}
</style>
